<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            width: 100px;
            height: 100px;
            background-color: #f00;
            position: absolute;
            border: 30px solid #000;
        }
    </style>
</head>

<body>

    <div id="a"></div>

    <script>

        // 鼠标跟随
        //    不断的设置盒子的  left / top 值  改变盒子的位置    盒子必须使用定位
        //    鼠标在移动  

        var oDiv = document.querySelector('div');
        var r = oDiv.offsetWidth / 2;

        document.addEventListener('mousemove', function (e) {
            var x = e.x - r;
            var y = e.y - r;
            oDiv.style.left = x + 'px';
            oDiv.style.top = y + 'px';
        })

    </script>

</body>

</html>